<?php
class Documents extends Admin_Controller
{
	function __construct()
	{
		parent::__construct();
	}
	
	function index()
	{
		$data['documents'] = new Document();
		if(@$_GET['search'])$data['documents']->where("title like '%".$_GET['search']."%'");
		if(@$_GET['status'])$data['documents']->where('status',$_GET['status']);
		if(@$_GET['category_id'])$data['documents']->where("category_id = ".$_GET['category_id']);
		
		$data['documents']->order_by('id','desc')->get_page();
		$this->template->append_metadata(js_lightbox());
		$this->template->append_metadata(js_checkbox('approve'));
		$this->template->build('admin/document_index',$data);
	}
	
	function form($id=FALSE)
	{
		$data['document'] = new Document($id);
		$this->template->append_metadata(js_datepicker());
		$this->template->build('admin/document_form',$data);
	}
	
	function save($id=false){
		if($_POST){
			$document = new Document($id);
			if(!$id)$_POST['user_id'] = $this->session->userdata('id');
			$_POST['start_date'] = Date2DB($_POST['start_date']);
			$_POST['end_date'] = Date2DB($_POST['end_date']);
			$document->from_array($_POST);
			$document->save();
			set_notify('success', lang('save_data_complete'));
		}
		redirect($_POST['referer']);
	}
	
	function approve($id)
	{
		if($_POST)
		{
			$document = new Document($id);
			$_POST['approve_id'] = $this->session->userdata('id');
			$document->approve_date = date("Y-m-d H:i:s");
			$document->from_array($_POST);
			$document->save();
			echo approve_comment($document);
		}

	}
}
?>